package com.zhanghe.wzq.data;

import com.zhanghe.wzq.Game;
import com.zhanghe.wzq.Table;
import com.zhanghe.wzq.User;
import io.netty.util.internal.ConcurrentSet;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * TableManager
 *
 * @author Clevo
 * @date 2018/12/25
 */
@Slf4j
public class TableManager {

    public static TableManager INSTANCE = new TableManager();

    private TableManager(){
        tableIdGenerator = new AtomicLong();
        tableMap = new ConcurrentHashMap<String, Table>();
        userTableMap = new ConcurrentHashMap<String, Table>();
    }

    private AtomicLong tableIdGenerator;
    /**
     * 所有的桌map
     */
    private Map<String,Table> tableMap;
    /**
     * 用户所在的桌map
     */
    private Map<String,Table> userTableMap;

    public Table getUserTable(String userId){
        return userTableMap.get(userId);
    }

    public Table getTableById(String tableId){
        return tableMap.get(tableId);
    }

    /**
     * 用户创建一个新的桌
     * @param userId
     * @return
     */
    public Table createNewTable(String userId){
        Table table = new Table();
        String tableId = String.valueOf(tableIdGenerator.addAndGet(1));
        table.setId(tableId);
        Set<String> users = new ConcurrentSet<String>();
        users.add(userId);
        table.setUsers(users);
        tableMap.put(tableId,table);
        userTableMap.put(userId,table);
        return table;
    }

    /**
     * 用户退出此桌
     * @param userId
     */
    public void exitTable(String userId,String tableId){
        Table table = tableMap.get(tableId);
        if(table != null){
            if(table.getUsers().contains(userId)){
                table.getUsers().remove(userId);
                userTableMap.remove(userId);
            }
            if(table.getUsers().size() == 0){
                log.debug("桌:[{}]已经没有用户了.",tableId);
                tableMap.remove(tableId);
            }
        }
    }

    /**
     * 用户加入此桌
     * @param userId
     * @param tableId
     * @return
     */
    public boolean joinTable(String userId,String tableId){
        Table table = tableMap.get(tableId);
        if(table != null){
            if(table.getUsers().size() < 2){
                table.getUsers().add(userId);
                userTableMap.put(userId,table);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前房间列表
     * @return
     */
    public List<Table> getTableList(){
        List<Table> tables = new ArrayList<Table>();
        for (Map.Entry<String,Table> entry : tableMap.entrySet()){
            tables.add(entry.getValue());
        }
        Collections.sort(tables);
        return tables;
    }

    /**
     * 在桌上开始新一盘游戏
     */
    public StartGameVO startGame(String tableId){
        StartGameVO startGameVO = new StartGameVO();
        Table table = tableMap.get(tableId);
        if(table.getUsers().size()==2 && table!= null && ((table.getGame()!=null && table.getGame().isOver()) || table.getGame()==null)){
            Random r = new Random(System.currentTimeMillis());
            int random = r.nextInt(2);
            Object[] users = table.getUsers().toArray();
            String white = (String) users[random];
            String black;
            if(random == 0){
                black = (String)users[1];
            }else{
                black = (String)users[0];
            }
            Game game = new Game(black,white,15);
            table.setGame(game);
            startGameVO.setSuccess(true);
            return startGameVO;
        }else{
            startGameVO.setSuccess(false);
            return startGameVO;
        }
    }

    @Data
    public class StartGameVO{
        public boolean success;
        public String msg;
    }

    public static void main(String[] args) {
        Random r = new Random(System.currentTimeMillis());
        for(int i=0;i<100;i++) {
            System.out.println(r.nextInt(2));
        }
    }
}
